package com.hhsj.dynamicui.model;

import com.google.gson.annotations.SerializedName;
import com.hhsj.dynamicui.model.base.JSONAble;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.List;

/**
 * 一个组，或者叫模块，作为主视图的第一级子视图
 */
public final class GroupModel implements Serializable, JSONAble {

    @SerializedName("group_id")
    private String groupId;//组id

    @SerializedName("page_id")
    private String pageId;

    @SerializedName("rank_no")
    private int rankNo;

    @SerializedName("group_name")
    private String groupName;

    @SerializedName("group_desc")
    private String groupDesc;

    private ViewBean property;

    //组标题
    @SerializedName("title")
    private TitleViewBean titleView;

    @SerializedName("widget_list")
    private List<ModuleBean> businessModule;

    public String getPageId() {
        return pageId;
    }

    public void setPageId(String pageId) {
        this.pageId = pageId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getGroupDesc() {
        return groupDesc;
    }

    public void setGroupDesc(String groupDesc) {
        this.groupDesc = groupDesc;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public int getRankNo() {
        return rankNo;
    }

    public void setRankNo(int rankNo) {
        this.rankNo = rankNo;
    }

    public ViewBean getProperty() {
        return property;
    }

    public void setProperty(ViewBean property) {
        this.property = property;
    }

    public TitleViewBean getTitleView() {
        return titleView;
    }

    public void setTitleView(TitleViewBean titleView) {
        this.titleView = titleView;
    }

    public List<ModuleBean> getBusinessModule() {
        return businessModule;
    }

    public void setBusinessModule(List<ModuleBean> businessModule) {
        this.businessModule = businessModule;
    }

    @Override
    public String toJSONString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("group_id", groupId);
        jsonObject.put("page_id", pageId);
        jsonObject.put("rank_no", rankNo);
        jsonObject.put("group_name", groupName);
        jsonObject.put("group_desc", groupDesc);

        if (property != null) {
            jsonObject.put("property", new JSONObject(property.toJSONString()));
        }

        if (titleView != null) {
            jsonObject.put("title", new JSONObject(titleView.toJSONString()));
        }

        if (businessModule != null && businessModule.size() > 0) {

            JSONArray array = new JSONArray();
            for (int i = 0, size = businessModule.size(); i < size; i++) {
                JSONObject object = new JSONObject(businessModule.get(i).toJSONString());
                array.put(object);
            }
            jsonObject.put("widget_list", array);
        }

        return jsonObject.toString();
    }
}
